<?php
class HttpRewrite
{
    private $urlPage;
    private $urlFileHtacess;
    private $reWrite;
    private $isReWrite;
    private $queryString;

    public function __construct($fileContent = null, $baseUrl = null, $isReWrite = false)
    {
        $this->reWrite = $fileContent;
        $this->urlPage = $baseUrl;
        $this->isReWrite = $isReWrite;
    }

    public function checkQueryString()
    {
        $queryString = explode('?', $_SERVER['REQUEST_URI']);
        $queryString = explode('&', $queryString[1]);
        foreach ($queryString as $k => $v) {
            $ex = explode('=', $v);
            $queryString[$k] = $ex;
        }
        $this->queryString = $queryString;
    }

    public function setIsReWrite($isReWrite = false)
    {
        $this->isReWrite = $isReWrite;
    }

    public function getIsReWrite()
    {
        return $this->isReWrite;
    }

    public function setReWrite($fileContent)
    {
        $this->reWrite = $fileContent;
    }

    public function getAllUrl()
    {
        return $this->reWrite;
    }

    public function getUrl($urlRewire = null, $parameter = null)
    {
        $url = $this->reWrite[$urlRewire];
        if ($this->isReWrite) {
            return $this->replaceUrlReWrite($url['urlRewire'], $parameter);
        } else {
            return $this->replaceUrlBasic($url['urlBasic'], $parameter);
        }
    }

    public function getFullUrl($urlRewire = null, $parameter = null)
    {
        $urlRewire = (array_key_exists($urlRewire, $this->reWrite))?$urlRewire:"index";
        $url = $this->reWrite[$urlRewire];
        if ($this->isReWrite) {
            return $this->urlPage . $this->replaceUrlReWrite($url['urlRewire'], $parameter);
        } else {
            return $this->urlPage . $this->replaceUrlBasic($url['urlBasic'], $parameter);
        }
    }

    private function replaceUrlReWrite($url, $parameter)
    {

        $url = str_replace('^', '', $url);
        $url = str_replace('$', '', $url);

        if ($parameter == null) {
            return $url;
        } else {
            $url = str_replace('([0-9]+)', '(.*)', $url);
            $url = explode('(.*)', $url);
            $i = 0;
            $string = '';
            foreach ($url as $k) {
                if(array_key_exists($i, $parameter)) {
                    $string .= $k . $parameter[$i];
                    $i++;
                } else
                    $string .= $k;
            }
            return $string;
        }
    }


    private function replaceUrlBasic($url, $parameter)
    {
        if ($parameter != null) {
            $i = 0;
            foreach ($parameter as $k) {
                $i++;
                $url = str_replace('$' . $i, $k, $url);
            }
        }
        return $url;
    }

    public function createFileHtaccess()
    {
        $ourFileName = ".htaccess";
        if (file_exists($this->urlFileHtacess . $ourFileName)) {
            return false;
        } else {
            if ($this->urlFileHtacess != "")
                mkdir($this->urlFileHtacess, 0777); //create file

            //content file .htaccess
            $content = "#Options All -Indexes\n";
            $content .= "Options +FollowSymLinks\n";
            $content .= "RewriteEngine On\n#Redirect non-www to www\n";
            $content .= "#RewriteCond %{HTTP_HOST} !^domain.com$ [NC]\n";
            $content .= "#RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]\n";

            $content .= "\n";

            $content .= "#URL Rewrite\n";
            foreach ($this->reWrite as $k => $v) {
                $content .= "RewriteRule " . $v['urlRewire'] . " " . $v['urlBasic'] . " [L]\n";
            }

            //create file and write gile .htaccess

            //create file
            $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
            //write content file
            fwrite($ourFileHandle, $content);
            fclose($ourFileHandle);

            return true;
        }

    }

    public function setQueryString($queryString)
    {
        $this->queryString = $queryString;
    }

    public function getQueryString()
    {
        return $this->queryString;
    }

    public function setUrlPage($urlPage)
    {
        $this->urlPage = $urlPage;
    }

    public function getUrlPage()
    {
        return $this->urlPage;
    }
}
